//
//  WmcSyncSettingsListCell.swift
//  YPWatermarkCamera
//
//  Created by 周诗松 on 2022/9/17.
//

import UIKit

class WmcSyncSettingsListCell: WmcBaseTableCell {
    // Internal

    var onOffSwitchClosure: (((model: WmcMediaAlbumConfigurationModel, isOn: Bool)) -> Void)?

    override func makeUI() {
        contentView.addSubview(personalAvatarView)
        contentView.addSubview(teamAvatarView)
        contentView.addSubview(teamNameLabel)
        contentView.addSubview(onOffSwitch)
        contentView.addSubview(line)

        teamAvatarView.addSubview(teamAbbreviationLabel)

        personalAvatarView.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.width.height.equalTo(32)
            make.centerY.equalToSuperview()
        }

        teamAvatarView.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.width.height.equalTo(32)
            make.centerY.equalToSuperview()
        }

        teamAbbreviationLabel.snp.makeConstraints { make in
            make.left.right.top.bottom.equalToSuperview()
        }

        teamNameLabel.snp.makeConstraints { make in
            make.left.equalTo(56)
            make.centerY.equalToSuperview()
            make.right.equalTo(onOffSwitch.snp.left).offset(-30)
        }

        onOffSwitch.snp.makeConstraints { make in
            make.width.equalTo(48)
            make.height.equalTo(27)
            make.right.equalTo(-16)
            make.centerY.equalToSuperview()
        }
        
        line.snp.makeConstraints { make in
            make.left.equalTo(17)
            make.right.equalTo(-17)
            make.bottom.equalTo(0)
            make.height.equalTo(1)
        }
    }

    func bindModel(_ model: WmcMediaAlbumConfigurationModel) {
        self.model = model
        
        if model.albumType == 0 {
            personalAvatarView.isHidden = false
            teamAvatarView.isHidden = true
            teamNameLabel.text = "个人云相册"
        } else {
            personalAvatarView.isHidden = true
            teamAvatarView.isHidden = false
            teamAbbreviationLabel.text = String(model.name.suffix(2))
            teamNameLabel.text = model.name
        }
        
        onOffSwitch.isOn = model.isSync
    }

    // Private

    private var model: WmcMediaAlbumConfigurationModel?

    private lazy var personalAvatarView: UIImageView = {
        let view = UIImageView(image: "personal_album_icon".wmc_image)
        return view
    }()

    private lazy var teamAvatarView: UIView = {
        let view = UIView()
        view.backgroundColor = .wmc_blue_5290FD
        view.layer.cornerRadius = 4
        return view
    }()

    private lazy var teamAbbreviationLabel: UILabel = {
        let label = UILabel()
        label.font = 12.wmc_font
        label.textColor = .wmc_white_FFFFFF
        label.textAlignment = .center
        return label
    }()

    private lazy var teamNameLabel: UILabel = {
        let label = UILabel()
        label.font = 17.wmc_font
        label.textColor = .wmc_grayH2_323233
        return label
    }()

    private lazy var onOffSwitch: UISwitch = {
        let view = UISwitch()
        view.onTintColor = .wmc_blue_5290FD
        view.tintColor = .wmc_green_E6F8E6
        view.addTarget(self, action: #selector(switchDidChange(sender:)), for: .valueChanged)
        return view
    }()

    private lazy var line: UIView = {
        let view = UIView()
        view.backgroundColor = .wmc_grayH19_F5F5F5
        return view
    }()

    @objc
    private func switchDidChange(sender: UISwitch) {
        guard let model = model else { return }
        onOffSwitchClosure?((model: model, isOn: sender.isOn))
    }
}
